$.root_ = $('body');
$.navAsAjax = true;
var mainTree;
var zNodes;

var baseCode = "";
var ossCode = "";
var buildCode = "";
var moduleCode = "";
var floorCode = "";
var roomCode = "";
var baseName = "";
var ossName = "";
var buildName = "";
var moduleName = "";
var floorName = "";
var roomName = "";

var roomType = "";


$(document).ready(function () {

    initApp.SmartActions();
    loadTreeData();
    $(window).resize();

});


/*  显示隐藏左边树开始 */

initApp = function (a) {
	console.log(new Date(),' planeMain.js function initApp');
    return a.addDeviceType = function () {
        return $('body').addClass("desktop-detected")
    },
        a.SmartActions = function () {
            var a = {
                "minifySecondMenu": function (a) {
                    if ($('body').hasClass("minified")) {
                        $('body').toggleClass("minifySecond"), a.effect("highlight", {}, 500)
                    } else {
                        $('body').toggleClass("second"), a.effect("highlight", {}, 500)
                    }
                    $(window).resize();
                }
            };
            $('[data-action="minifySecondMenu"]').on("click", function (b) {
                var c = $(this);
                a.minifySecondMenu(c),
                    b.preventDefault(),
                    c = null
            })

        }, a
}({});

$(window).resize(function () {
	console.log('planeMain.js function resize ');
    $('.gw').height($('#left-sub-panel').height());
    if($('body').hasClass("second")){
        $('.gw').width(window.screen.width)

    }else{
        $('.gw').width(window.screen.width-215)
    }
    // jqgridResize();
})

/*  显示隐藏左边树结束 */

/*  树操作 开始 */
var setting = {

    //check: {
    //    enable: true
    //},
    view: {
        selectedMulti: false,
        dblClickExpand: false,
        showLine: false
    },
    data: {
        key: {
            title: "name"
        },
        simpleData: {
            enable: true
            //checked: true
        }
    },
    callback: {
        onClick: function(treeId, treeNode) {
	alert('planeMain.js function onClick: function(treeId, treeNode) ' + treeId + ' treeNode ' + treeNode );

            var selectedNode = mainTree.getSelectedNodes()[0];
            console.log(selectedNode);
            initAreaData(selectedNode);
            // if( $(".gw #sandboxIframe")[0] ==undefined){
            //     //页面展示的事资源信息，而非可视化信息
            //     window.location.search ? window.location.href = window.location.href.replace(window.location.search, "").replace(window.location.hash, "") + "#/resource/plan?type="+selectedNode.ciType+"&code="+selectedNode.id+"&isViewStatistics=0" : window.location.hash = "#/resource/plan?type="+selectedNode.ciType+"&code="+selectedNode.id+"&isViewStatistics=0" ;
            //     $(".res_content").css('top','40px');
            // }else{
            //     var sandboxIframe = window.frames["sandboxIframe"];
            //     if(sandboxIframe != undefined && sandboxIframe.document.readyState=="complete"){
            //         changeSpaceNode(sandboxIframe,sandboxIframe.world,selectedNode.ciType,ossCode,buildCode,floorCode,roomCode);
            //     }
            // }
            changeSpaceNode(world,selectedNode.ciType,ossCode,buildCode,floorCode,roomCode);

        }
    }
};





function  loadTreeData(){
	console.log(new Date(),' planeMain.js function loadTreeData ');
    $.ajax({
        "type": "POST",
        "url": ctx + "/resource/mainTree?isTree=1",
        "dataType": "json",
        async : false,
        "success": function (data) {
            if(data.success){
                var obj = eval('(' + data.data + ')');
                zNodes = obj;
            }
        },
        "error": function (data) {
            alertErr(data.message,"提示");
        }

    });
    mainTree = $.fn.zTree.init($("#planeLeftTree"), setting, zNodes);
    var key = $("#key");
    key.bind("focus", focusKey).bind("blur", {tree: mainTree}, blurKey).bind("change cut input propertychange", searchNode(key, mainTree));
    key.bind('keyup', function (e) {
        searchNode(key, mainTree);
    });
    var node = mainTree.getNodes()[0];
    mainTree.selectNode(node);
}

function initAreaData(selectedNode){	
	console.log(new Date(),' planeMain.js function initAreaData ',JSON.stringify( selectedNode));
    if(selectedNode!=undefined){
        if(selectedNode.ciType == "base"){
            baseCode = selectedNode.id;
            baseName = selectedNode.name;
            ossCode = "";
            ossName = "";
            buildCode = "";
            buildName = "";
            moduleCode = "";
            moduleName = "";
            floorCode = "";
            floorName = "";
            roomCode = "";
            roomName = "";
            roomType = "";
        }

        if(selectedNode.ciType == "oss"){
            baseCode = selectedNode.baseCode;
            baseName = selectedNode.baseName;
            ossCode = selectedNode.id;
            ossName = selectedNode.name;
            buildCode = "";
            buildName = "";
            moduleCode = "";
            moduleName = "";
            floorCode = "";
            floorName = "";
            roomCode = "";
            roomName = "";
            roomType = "";
        }

        if(selectedNode.ciType == "build"){
            baseCode = selectedNode.baseCode;
            baseName = selectedNode.baseName;
            ossCode = selectedNode.ossCode;
            ossName = selectedNode.ossName;
            buildCode = selectedNode.id;
            buildName = selectedNode.name;
            moduleCode = "";
            moduleName = "";
            floorCode = "";
            floorName = "";
            roomCode = "";
            roomName = "";
            roomType = "";
        }

        if(selectedNode.ciType == "module"){
            baseCode = selectedNode.baseCode;
            baseName = selectedNode.baseName;
            ossCode = selectedNode.ossCode;
            ossName = selectedNode.ossName;
            buildCode = selectedNode.buildCode;
            buildName = selectedNode.buildName;
            moduleCode = selectedNode.id;
            moduleName = selectedNode.name;
            floorCode = "";
            floorName = "";
            roomCode = "";
            roomName = "";
            roomType = "";
        }

        if(selectedNode.ciType == "floor"){
            baseCode = selectedNode.baseCode;
            baseName = selectedNode.baseName;
            ossCode = selectedNode.ossCode;
            ossName = selectedNode.ossName;
            buildCode = selectedNode.buildCode;
            buildName = selectedNode.buildName;
            moduleCode = selectedNode.moduleCode;
            moduleName = selectedNode.moduleName;
            floorCode = selectedNode.id;
            floorName = selectedNode.name;
            roomCode = "";
            roomName = "";
            roomType = "";
        }

        if(selectedNode.ciType == "room"){
            baseCode = selectedNode.baseCode;
            baseName = selectedNode.baseName;
            ossCode = selectedNode.ossCode;
            ossName = selectedNode.ossName;
            buildCode = selectedNode.buildCode;
            buildName = selectedNode.buildName;
            moduleCode = selectedNode.moduleCode;
            moduleName = selectedNode.moduleName;
            floorCode = selectedNode.floorCode;
            floorName = selectedNode.floorName;
            roomCode = selectedNode.id;
            roomName = selectedNode.name;
            roomType = selectedNode.roomType;
        }
    }


}

function changeSelTreeNode(code){
	console.log('planeMain.js function changeSelTreeNode ', code);
    var node = mainTree.getNodeByParam("id",code);
    mainTree.selectNode(node);
    initAreaData(node);
}
var changeSpaceNode = function (world,ciType,oss,build,floor,room){
	console.log('planeMain.js function changeSpaceNode () ');
	console.log(' changeSpaceNode world ', world);
	console.log(' changeSpaceNode ciType ', ciType);
	console.log(' changeSpaceNode oss ', oss);
	console.log(' changeSpaceNode build ', build);
	console.log(' changeSpaceNode floor ', floor);
	console.log(' changeSpaceNode room ', room);
    if(ciType == 'base'){
        hideRoomView();
        world.redirect([]);
    }else if(ciType == 'oss'){
        hideRoomView();
        world.redirect([oss]);
    }
    else if(ciType == 'build'){
        hideRoomView();
        world.redirect([oss,build]);
    }
    else if(ciType == 'floor'){
        hideRoomView();
        world.redirect([oss,build,floor]);
    }
    else if(ciType == 'room'){
        world.redirect([oss,build,floor,room]);
    }
}

/*  树操作 结束 */

/*  三维 开始 */

function changeAreaData(obj){
	console.log('planeMain.js function changeAreaData ');
	console.log(' changeAreaData obj ', JSON.stringify(obj));
    var type = obj.arrangementType;
    if(type == "base"){
        ossCode = "";
        ossName = "";
        buildCode = "";
        buildName = "";
        moduleCode = "";
        moduleName = "";
        floorCode = "";
        floorName = "";
        roomCode = "";
        roomName = "";
        roomType = "";
    }

    if(type == "oss"){
        if(obj!=undefined){
            ossCode = obj.code;
            ossName = obj.name;
        }
        buildCode = "";
        buildName = "";
        moduleCode = "";
        moduleName = "";
        floorCode = "";
        floorName = "";
        roomCode = "";
        roomName = "";
        roomType = "";
    }

    if(type == "build"){
        if(obj.parent!=undefined){
            ossCode = obj.parent.code;
            ossName = obj.parent.name;
        }
        if(obj!=undefined){
            buildCode = obj.code
            buildName = obj.name;
        }
        moduleCode = "";
        moduleName = "";
        floorCode = "";
        floorName = "";
        roomCode = "";
        roomName = "";
        roomType = "";
    }



    if(type == "floor"){
        if(obj.parent.parent!=undefined){
            ossCode = obj.parent.parent.code;
            ossName = obj.parent.parent.name;
        }
        if(obj.parent!=undefined){
            buildCode = obj.parent.code
            buildName = obj.parent.name;
        }
        if(obj!=undefined){
            floorCode = obj.code
            floorName = obj.name;
        }
        roomCode = "";
        roomName = "";
        roomType = "";
    }

    if(type == "room"){
        if(obj.parent.parent.parent!=undefined){
            ossCode = obj.parent.parent.parent.code;
            ossName = obj.parent.parent.parent.name;
        }
        if(obj.parent.parent!=undefined){
            buildCode = obj.parent.parent.code
            buildName = obj.parent.parent.name;
        }
        if(obj.parent!=undefined){
            floorCode = obj.parent.code
            floorName = obj.parent.name;
        }
        if(obj!=undefined){
            roomCode = obj.code
            roomName = obj.name;
        }
    }

}

/**
 * 面包屑回退方法
 * @param type 跳转到的层次
 * @param ossName 数据中心名称
 * @param buildName 楼栋名称
 * @param floorName 楼层名称
 */
function dealBack(type){
	console.log('planeMain.js function dealBack type ' + type);
    var html ='';
    html = getCrumbsHtml(type) ;
	console.log(' getCrumbsHtml html ',  html);
    $("#gwCrumbs").html("<span>" + html + "</span>");
    if (type == 'room'){
        swtichTypeDiv("planeBuildRoomDiv",false);
        buildLoadRoom(roomCode,roomName);
    }else if (type == 'floor'){
        $(".div_index").css("visibility","hidden");
        $(".div_index").css("height","0px");
        $("#ossDom").css("visibility","visible");
        purposeCode = floorCode;
        gwWorld.redirect([ossCode,buildCode,floorCode]);
    } else if (type == 'build'){
        $(".gwleft").show();
        $(".gwright").show();
        swtichTypeDiv("buildData",true);
        purposeCode = buildCode;
        gwWorld.redirect([ossCode,buildCode]);
    } else if(type == 'oss'){
        $(".gwleft").hide();
        $(".gwright").hide();
        // $("li.act").trigger ("click");
        $(".resDiv").show();
        swtichTypeDiv("parkData",true);
        purposeCode = ossCode;
        gwWorld.redirect([ossCode]);
    } else if(type == 'base'){
        purposeCode = "";
        swtichTypeDiv("worldData",true);
        gwWorld.redirect([]);
    }

}
function getCrumbsHtml(type){
	console.log('planeMain.js function getCrumbsHtml type ' + type);
    var html ='';
    if(type == 'room'){
        html = "<a href='#' style='color: #00fefe' onclick='dealBack(\"base\");'>全国数据中心</a> > " +
            "<a href='#' style='color: #00fefe' onclick='dealBack(\"oss\");'>" + ossName +
            "</a> > " + "<a href='#' style='color: #00fefe' onclick='dealBack(\"build\");'>" + buildName +
            "</a> > " + "<a href='#' style='color: #00fefe' onclick='dealBack(\"floor\");'>" + floorName +
            "</a> > " + roomName;
    }else if (type == 'floor'){
        html = "<a href='#' style='color: #00fefe' onclick='dealBack(\"base\");'>全国数据中心</a> > " +
            "<a href='#' style='color: #00fefe' onclick='dealBack(\"oss\");'>" + ossName +
            "</a> > " + "<a href='#' style='color: #00fefe' onclick='dealBack(\"build\");'>" + buildName +
            "</a> > " + floorName;
    } else if (type == 'build'){
        html = "<a href='#' style='color: #00fefe' onclick='dealBack(\"base\");'>全国数据中心</a> > " +
            "<a href='#' style='color: #00fefe' onclick='dealBack(\"oss\");'>" + ossName +
            "</a> > " + buildName;
    } else if(type == 'oss'){
        html = "<a href='#' style='color: #00fefe' onclick='dealBack(\"base\");' ossCode='' ossName='' >全国数据中心</a> > "+ ossName ;
    }
    else if(type == 'base'){
    }
    return html;
}
function  gotoDealBack(){
	console.log('planeMain.js function gotoDealBack ');
    $(this).attr("ossCode")
    dealBack(type,ossCode, ossName,buildCode,buildName,floorCode,floorName,roomCode,roomName);
}
/*  三维 结束*/

/**
 * 处理三维显示数据
 */
function deal3DViewData(planeMainList){
	console.log('planeMain.js function deal3DViewData ');
	console.log(' deal3DViewData ', JSON.stringify(planeMainList));
    if (planeMainList == null || planeMainList == undefined){
        console.log("未获取到数据，无法展示");
        return;
    }
    let result = {};
    let ossList = [];
    let buildList = [];
    let floorList = [];
    let roomList = [];
   
    $.each(planeMainList,function (i, plane) {
        let type = plane.arrangement_type;
		console.log(' $.each(planeMainList type ', type,  JSON.stringify(plane));
        if (plane.conf_json != '' && plane.conf_json != undefined){
            let confJson =JSON.parse(plane.conf_json);
            confJson.code = plane.code;
            confJson.name = plane.name;
            confJson.parentId = plane.parent_code;
			
            if(plane.enable==0){
                confJson.enable = false;
            }else{
                confJson.enable = true;
            }
            // result.camPos = JSON.parse(plane.cam_pos);
            if (type == 'base'){
                confJson.src = china;
                result = confJson;
				console.log(' $.each(planeMainList base result2 ', JSON.stringify( JSON.stringify(result)));
            }else if (type == 'oss'){
                if(confJson.tipUrl != '' && confJson.tipUrl != undefined){
                    confJson.tipUrl = ctx+confJson.tipUrl;
                }
                ossList.push(confJson);
				console.log(' $.each(planeMainList oss ossList ', JSON.stringify(ossList));
            }else if (type == 'build'){
                if(confJson.tipUrl != '' && confJson.tipUrl != undefined){
                    confJson.tipUrl = createBuildingInfo;
                }
                buildList.push(confJson);
				console.log(' $.each(planeMainList build buildList ', JSON.stringify(buildList));
            }else if (type == 'floor'){
                if(confJson.boxTopMap != '' && confJson.boxTopMap != undefined){
                    confJson.boxTopMap =ctxStatic+confJson.boxTopMap;
                }
                if(confJson.maps != '' && confJson.maps != undefined){
                    confJson.maps =ctxStatic+confJson.maps;
                }
                if(confJson.map != '' && confJson.map != undefined){
                    confJson.map =ctxStatic+confJson.map;
                }
                floorList.push(confJson);
				console.log(' $.each(planeMainList floor floorList ', JSON.stringify(floorList));
            }else{
                if(confJson.map != '' && confJson.map != undefined){
                    confJson.map =ctxStatic+confJson.map;
                }
                roomList.push(confJson);
				console.log(' $.each(planeMainList else roomList ', JSON.stringify(roomList));
            }
        }
    })
   
    //对各层级列表进行数据整合
    console.log(' 对各层级列表进行数据整合 ossList ', i, JSON.stringify(JSON.stringify(ossList)));
    $.each(ossList,function (i, oss) {
        console.log(' 对各层级列表进行数据整合 oss ', i, JSON.stringify(oss));
        let builds = [];
		console.log(' 对各层级列表进行数据整合 buildList ', i, JSON.stringify(JSON.stringify(buildList)));
        $.each(buildList,function (j, build) {
			if(j < 3){
				console.log(' 对各层级列表进行数据整合 build ', j, JSON.stringify(build));
			}
            let floors = [];
			console.log(' 对各层级列表进行数据整合 floorList ', j, JSON.stringify(JSON.stringify(floorList)));
            $.each(floorList, function (k, floor){
				if(k < 3){
					console.log(' 对各层级列表进行数据整合 floor ', k, JSON.stringify(floor));
				}
                let rooms = [];
				console.log(' 对各层级列表进行数据整合 roomList ', k, JSON.stringify(JSON.stringify(roomList)));
                $.each(roomList, function (l, room) {
					if(l < 3){
						console.log(' 对各层级列表进行数据整合 room ', l, JSON.stringify(room));
					}
                    if (room.parentId == floor.code){
                        rooms.push(room);
                    }
                })
                floor.nodes = rooms;
                if (floor.parentId == build.code){
                    floors.push(floor);
                }
            })
            if (floors.length > 0){
                build.nodes = floors;
            }
            if (build.parentId == oss.code){
                builds.push(build);
            }
        })
        oss.nodes = builds;
		if(i < 2){
			console.log(' 对各层级列表进行数据整合完成的oss ', JSON.stringify(JSON.stringify(oss)));
		}
    })
    result.nodes = ossList;
	console.log(' deal3DViewData最终返回的数据result ', JSON.stringify(JSON.stringify(JSON.stringify(result))));
    return result;
}
